package org.jsbd.boss.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMiguReadConfigDao;
import org.jsbd.boss.domian.MiguReadConfigBean;
import org.jsbd.boss.service.IMiguReadConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.TimeModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("miguReadConfigService")
public class MiguReadConfigServiceImpl implements IMiguReadConfigService {

	@Autowired
	private IMiguReadConfigDao miguReadConfigDao;
	@Autowired
	private RedisClient<String,Object> redisClientMigu;

	@Override
	public void saveMiguReadConfig(MiguReadConfigBean miguReadConfig){
		if(miguReadConfig.getTotalNum()==null){
			miguReadConfig.setTotalNum(0L);
		}
		miguReadConfig.setDeleteFlag(0);
		miguReadConfig.setStatus(0);
		miguReadConfigDao.saveMiguReadConfig(miguReadConfig);
	}

	@Override
	public void updateMiguReadConfig(MiguReadConfigBean miguReadConfig){
		if(miguReadConfig.getTotalNum()==null){
			miguReadConfig.setTotalNum(0L);
		}
		miguReadConfigDao.updateMiguReadConfig(miguReadConfig);
	}

	@Override
	public MiguReadConfigBean getMiguReadConfig(MiguReadConfigBean miguReadConfig) {
		return miguReadConfigDao.getMiguReadConfig(miguReadConfig);
	}

	@Override
	public List<MiguReadConfigBean> getMiguReadConfigList(MiguReadConfigBean miguReadConfig) {
		return miguReadConfigDao.getMiguReadConfigList(miguReadConfig);
	}

	@Override
	public Map<String, Object> getMiguReadConfigListPage(DataGridModel page, MiguReadConfigBean miguReadConfig) {
		Map<String,Object> results=miguReadConfigDao.getMiguReadConfigListPage(page, miguReadConfig);
		
		List<MiguReadConfigBean> configList=(List<MiguReadConfigBean>)results.get("rows");
		if(configList==null||configList.isEmpty()){
			return results;
		}
		
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUREAD_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUREAD_NEW_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUREAD_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUREAD_NEW_TASK_NUM_DONE;
		
		String doDay=DateUtil.getCurrentDateStr();

		for(MiguReadConfigBean config:configList){
			String suffixKey=doPrefixKey+config.getChannelId()+"_"+config.getVersion()+"_"+doDay;
			Long userNum=(Long) doNumMap.get(suffixKey);
			if(userNum==null){
				userNum=0L;
			}
			long doNum=userNum;
			String successKey=sucessPrefixKey+","+config.getChannelId()+","+config.getVersion()+","+doDay;
			String successNum=(String)successMap.get(successKey);
			if(StringUtils.isBlank(successNum)){
				successNum="0";
			}
			
			Long totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0L;
			}
			int planNum=TimeModelUtils.genMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		
		return results;
	}

	@Override
	public Integer getMiguReadConfigCount(MiguReadConfigBean miguReadConfig) {
		return miguReadConfigDao.getMiguReadConfigCount(miguReadConfig);
	}
	
	@Override
	public void deleteMiguReadConfig(MiguReadConfigBean miguReadConfig) {
		miguReadConfigDao.deleteMiguReadConfig(miguReadConfig);
	}

	@Override
	public void updateMiguReadStatus(MiguReadConfigBean miguReadConfig) {
		miguReadConfigDao.updateMiguReadStatus(miguReadConfig);
	}
	
}
